package com.bobo.spring;

import jakarta.servlet.http.*;
import org.springframework.web.bind.annotation.*;

// HTTP请求是基础交互（点菜）。
// Cookie是客户端的身份凭证（会员卡号）。
// Session是服务端的真实数据（会员档案）。

@RequestMapping("/request2")
@RestController
// HttpServletRequest(请求) HttpServletResponse(响应)
// HttpServletRequest(请求):
// 正常通过方法,以name,age等获取信息,仅仅为http请求中的部分特定信息
// 而想要获取"整个http请求",就需要以HttpServletRequest来获取请求
public class RequestController2 {
    @RequestMapping("/getCookie")
    public String getCookie(HttpServletRequest request) {
        // 通过http请求获取cookie
        Cookie[] cookies = request.getCookies();
        // 官方文档说明:如果没Cookie,返回null,而不是空数组,因此不能通过(cookies.length != 0)来判断
        if(cookies != null) {
            for (Cookie cookie : cookies) {
                System.out.println(cookie.getName() + " : " + cookie.getValue());
            }
        }
        return "获取请求成功";
    }
    // @CookieValue:通过Cookie名来获取Cookie值
    // 默认为true,如果没有指定Cookie会报错,改成false即可避免(非必须)
    @RequestMapping("/getCookie2")
    public String getCookie2(@CookieValue(value = "class",required = false) String className){
        return "获取请求成功" + " : " + className;
    }
    // 如果此时没有JSESSIONID,通过setSession创建
    // 如果有JSESSIONID,则获取,并且修改信息
    // (每个浏览器会话(客户端实例)通常只有一个JSESSIONID)
    @RequestMapping("/setSession")
    public String setSession(HttpServletRequest request){
        HttpSession session = request.getSession();
        session.setAttribute("name","zhangsan");
        session.setAttribute("age",15);
        session.setAttribute("id",1111);
        return "设置session成功";
    }
    // 此时setSession创建的JSESSIONID存有三个元素:name,age,id
    // 再次调用setSession1时,查找JSESSIONID发现拥有name和age,则替换
    // 而setSession1中没有id,则原JSESSIONID中的id不变.
    @RequestMapping("/setSession1")
    public String setSession1(HttpServletRequest request){
        HttpSession session = request.getSession();
        session.setAttribute("name","lisi");
        session.setAttribute("age",18);
        return "设置session1成功";
    }
    @RequestMapping("/getSession")
    public String getSession(HttpServletRequest request){
        // 从cookie中获取sessionId,并根据sessionId获取httpSession对象
        // 如果没有session时,会创建一个session
        HttpSession session = request.getSession();
        String name = (String) session.getAttribute("name");
        Integer age = (Integer) session.getAttribute("age");
        Integer id = (Integer) session.getAttribute("id");
        return "获取session成功 name: " + name + ",age: " + age + ",id: " + id;
    }
    // 和上面的区别:request.getSession可以带参数(true/false),默认true,代表会创建session
    @RequestMapping("/getSession1")
    public String getSession1(HttpSession session){
        String name = (String) session.getAttribute("name");
        Integer age = (Integer) session.getAttribute("age");
        Integer id = (Integer) session.getAttribute("id");
        return "获取session成功 name: " + name + ",age: " + age + ",id: " + id;
    }
    // 获取指定属性name
    @RequestMapping("/getSession2")
    public String getSession2(@SessionAttribute("name") String name){
        return "name:" + name;
    }
    @RequestMapping("/getHeader")
    public String getHeader(HttpServletRequest request){
        String userAgent = request.getHeader("User-Agent");
        return "从header中获取数据,userAgent:" + userAgent;
    }
    @RequestMapping("/getHeader1")
    public String getHeader1(@RequestHeader("User-Agent") String userAgent){
        return "从header中获取数据,userAgent:" + userAgent;
    }
}





